package org.example.springbootwebexample.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.example.springbootwebexample.mapper.EmpMapper;
import org.example.springbootwebexample.pojo.Emp;
import org.example.springbootwebexample.pojo.PageBean;
import org.example.springbootwebexample.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class EmpServiceImpl implements EmpService {

   @Autowired
    private EmpMapper empMapper;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Long count = empMapper.count();
//        List<Emp> list = empMapper.list(page, pageSize);
//        Long start =((count - 1) * pageSize);
//        return new PageBean(start, list);
//    }



    //模糊分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        log.info("Service层作用 name={}, gender={},begin={},end={}",name,gender,begin,end);

        //1.设置分页参数
        PageHelper.startPage(page, pageSize);

        //2.执行查询
        List<Emp> empList =  empMapper.list(name, gender, begin, end);


        log.info("Mapper作用完成" +empList.toString());


        //3.获取分页结果
        Page<Emp> p = (Page<Emp>) empList;

        return new PageBean(p.getTotal(), p.getResult());

    }


    //根据id 删除员工
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    //新增员工
    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        log.info("添加{}成功", emp);
    }

    //根据id 查询员工数据
    @Override
    public Emp selectById(Integer id) {
        return empMapper.selectById(id);
    }

    //更新员工数据
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getEmpByUsernameAndPassword(emp);
    }
}



